import Vue from 'vue'
import VueRouter from 'vue-router'
import { getLocal } from '@/utils/local.js'
import store from '@/store/index.js'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/home',
    component: () => import('@/views/home/home.vue'),
    redirect: '/home/user',
    children: [
      {
        path: '/home/page',
        component: () => import('@/views/home/page/page.vue'),
        meta: { needTab: true }
      },
      {
        path: '/home/video',
        component: () => import('@/views/home/video/video.vue'),
        meta: { needToken: true, needTab: true }
      },
      {
        path: '/home/pageDetail',
        component: () => import('@/views/home/page/pageDetail')
      },

      {
        path: '/home/question',
        component: () => import('@/views/home/question/question.vue'),
        meta: { needToken: true, needTab: true }
      },
      // 搜索页面
      {
        path: '/home/questionSerch',
        component: () => import('@/views/home/question/questionSerch.vue')
      },
      {
        path: '/home/user',
        component: () => import('@/views/home/user/user.vue'),
        meta: { needToken: true, needTab: true }
      },
      {
        path: '/home/userInfo/',
        component: () => import('@/views/home/user/userInfo.vue'),
        meta: { needToken: true, needTab: true }
      },
      {
        path: '/home/userEditor',
        component: () => import('@/views/home/user/userEditor.vue'),
        meta: { needToken: true }
      },
      // 消息
      {
        path: '/home/userMessage',
        component: () => import('@/views/home/user/userMessage.vue')
      },
      // 实名
      {
        path: '/home/userReal',
        component: () => import('@/views/home/user/userReal.vue')
      },
      // 反馈
      {
        path: '/home/userFeedback',
        component: () => import('@/views/home/user/userFeedback.vue')
      },
      // 小智
      {
        path: '/home/userStudent',
        component: () => import('@/views/home/user/userStudent.vue')
      },
      // 系统
      {
        path: '/home/userSystem',
        component: () => import('@/views/home/user/userSystem.vue'),
        meta: { needTab: true }
      },
      // 收藏
      {
        path: '/home/userHistory',
        component: () => import('@/views/home/user/userHistory.vue'),
        meta: { needTab: true }
      },
      // 作品
      {
        path: '/home/userWork',
        component: () => import('@/views/home/user/userWork.vue'),
        meta: { needTab: true }
      },
      // 粉丝
      {
        path: '/home/userFans',
        component: () => import('@/views/home/user/userFans.vue'),
        meta: { needTab: true }
      },
      // 关注
      {
        path: '/home/userFocu',
        component: () => import('@/views/home/user/userFocu.vue'),
        meta: { needTab: true }
      }
    ]
  }
]

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  //  不需要登录的页面直接调转
  if (!to.meta.needLogin) {
    next()
  } else {
    // 判断是不是登录状态
    if (store.state.isLogin) {
      next()
    } else {
      if (getLocal('token')) {
        // 调用vuex异步方法更新数据
        store.dispatch('reUsreInfo')
        // 放行
        next()
      } else {
        router.push('/login')
      }
    }
  }
})

export default router
